package edu.ynu.se.xiecheng.achitectureclass.project.service;

import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;

import edu.ynu.se.xiecheng.achitectureclass.project.dao.SoilPHDataDAO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.Greenhouse;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.SoilPHData;
import edu.ynu.se.xiecheng.achitectureclass.project.utils.GreenhouseDeviceUtils;
import edu.ynu.se.xiecheng.achitectureclass.project.utils.SensorApiClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.HttpCookie;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SoilPHDataService extends LogicService<SoilPHDataDAO, SoilPHData, Long> {

    @Autowired
    private SensorApiClient sensorApiClient;

    @Autowired
    private GreenhouseDeviceUtils greenhouseDeviceUtils;


    public SoilPHDataService(SoilPHDataDAO dao) {
        super(dao);
    }

    /**
     * 从传感器API获取PH数据并存储到数据库
     */
    @Transactional
    public void fetchAndSavePHData() {
        // 示例 Box 编号和传感器 ID 列表
        String boxNo = "300822040454";
        List<String> sensorIds = List.of("327061586065758007");

        // 调用外部接口获取传感器数据
        List<Map<String, Object>> sensorData = sensorApiClient.fetchSensorData(sensorIds, boxNo);

        for (Map<String, Object> data : sensorData) {
            System.out.println("Sensor data: " + data);

            // 创建或更新大棚信息
            String boxId = data.get("boxId").toString(); // 大棚 ID
            Greenhouse greenhouse = greenhouseDeviceUtils.getOrCreateGreenhouse(boxId);

            // 创建或更新设备信息
            String sensorId = data.get("id").toString(); // 传感器设备 ID
            String name = data.get("name").toString();
            Device device = greenhouseDeviceUtils.getOrCreateDevice(sensorId, name, greenhouse);


            // 创建 PH 数据实例并设置属性
            SoilPHData phData = new SoilPHData();
            phData.setName(name); // 数据名称
            phData.setValue(Double.valueOf(data.get("value").toString())); // PH 值
            phData.setUnit("pH"); // 单位
            phData.setTimestamp(new Date()); // 数据时间
            phData.setDevice(device); // 关联设备

            // 保存 PH 数据
            dao.save(phData); // 使用具体的 PH 数据仓库保存
        }
    }


    /**
     * 获取最新的PH数据
     * @return
     */
    public SoilPHData getLatestPHData() {
        SoilPHData soilPHData = dao.findFirstByOrderByTimestampDesc();
        if (soilPHData != null){
            return soilPHData;
        }else
            return new SoilPHData();
    }

    /**
     * 评价PH数据的状态
     * @param ph
     * @return
     */
    public String evaluateStatus(Double ph) {
        if (ph == null) return "未知";
        if (ph >= 6.0 && ph <= 7.5) return "优";
        if (ph >= 3 && ph < 6.0 || ph > 7.5 && ph <= 8.0) return "良";
        return "差";
    }

}

